<?php

require_once  __DIR__ . '/includes/authenticate.php';
include_once './db_bb_connect.php';


?>

<div id="viewplayeractionpointscontent">
<h4>View Player Actionpoints</h4>


<form>
  
<select class="form-control" name="season" id="select1">
    
<?php

if (isset($_POST['season'])) {

  $setseason = $_POST['season'];

echo  "<option selected=\"selected\" value=\"$setseason\"> $setseason is selected</option>";
  
} else {
  echo  "<option selected=\"selected\" disabled=\"disabled\"> Select season</option>";
}


$sql = "SELECT DISTINCT season 
FROM teams 
ORDER BY season ASC";

$result = $db->query($sql);


while ($row = $result->fetch_assoc()) {
  $season = $row['season'];

  echo "<option value=\"$season\"> $season </option>";
}

?>
</select>

<br>    
          <select class="form-control" name="playerID" id="select2">
          <option value="" disabled="disabled" selected="selected">Select a player to view actionpoints of</option>

<?php

if (isset($_POST['season'])) {
  
  $season = $_POST['season'];

  include_once './db_bb_connect.php'; 

  $sql = "SELECT users.firstname, users.lastname, users.user_key, teams.season, teams.playerID
  FROM users
  JOIN teams
  ON users.user_key = teams.playerID
  WHERE teams.season = '$season'
  ORDER BY users.firstname";

  $result=$db->query($sql);

  while($row=$result->fetch_assoc()){ 

    $user_key=$row['user_key']; 
    $firstname=$row['firstname']; 
    $lastname=$row['lastname']; 
  
  echo "<option value=\"$user_key\">$firstname $lastname</option>";

};

}

  ?>

          </select>
    </div>
    </div>


</form>

<div id="row">
<div id="col-md-12">  
  <div id="fullplayeractionpoints"></div>
</div>
</div>


<script type="text/javascript">
  
$(document).on( "change","#select1", function( e ) {
  postData = $(this).serialize();
  $.ajax({
    type: "POST",
    url: "./player_actionpoints.php",
    data: postData,
    success: function(data) {
      $("#personalactionpoints").html(data);
    }
  }); 
  e.preventDefault();
});

$(document).on( "change","#select2", function( e ) {
  postData = $(this).serialize();
  $.ajax({
    type: "POST",
    url: "./includes/handlers/player_actionpoints_handler.php",
    data: postData,
    success: function(data) {
      $("#fullplayeractionpoints").html(data);
    }
  }); 
  e.preventDefault();
});


</script>